package com.hcy.controller.manager;

import com.hcy.common.CommonInfoBz;
import com.hcy.common.dto.response.CallCarConfigDTO;
import com.hcy.common.dto.response.GaoDeGfidsResponseDTO;
import com.hcy.common.entity.CallCarConfig;
import com.hcy.common.entity.GaodeGfids;
import com.hcy.common.service.impl.CallCarConfigServiceImpl;
import com.hcy.common.service.impl.GaodeGfidsServiceImpl;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicmodel.signl.Add;
import com.hcy.rabbitbasicmodel.signl.Delete;
import com.hcy.rabbitbasicmodel.signl.Update;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author huchenying
 * @Description
 * @Date 2022/5/12
 **/
@RestController
@RequestMapping("/operator")
@Slf4j
public class OperatorController {

    @Autowired
    CommonInfoBz commonInfoBz;
    @Autowired
    CallCarConfigServiceImpl callCarConfig;
    @Autowired
    GaodeGfidsServiceImpl gaodeGfidsService;

    /**
     * -------------------------- 叫车策略 ------------------------------
     */

    /**
     * @Author huchenying
     * @Description  获取叫车策略配置list
     * @Date 2022/5/12 23:46
     **/
    @PostMapping("/getCallConfigList")
    public ResponseData<List<CallCarConfigDTO>> getCallConfigList(){
        ResponseData<List<CallCarConfigDTO>> responseData = new ResponseData<>();
        List<CallCarConfigDTO> callCarConfig = commonInfoBz.getCallCarConfig();
        return responseData.success(callCarConfig);
    }


    /**
     * @Author huchenying
     * @Description  添加或更新叫车策略
     * @Date 2022/5/12 23:46
     **/
    @PostMapping("/insertOrUpdateCallConfig")
    public ResponseData<CallCarConfigDTO> insertOrUpdateCallConfig(@Validated(Add.class) @RequestBody CallCarConfigDTO callCarConfigDTO){
        ResponseData<CallCarConfigDTO> responseData = new ResponseData<>();
        CallCarConfig carConfig = new CallCarConfig();
        try {
            BeanUtils.copyProperties(callCarConfigDTO,carConfig);
            boolean b = callCarConfig.saveOrUpdate(carConfig);
            if(!b){
                return responseData.fail("操作失败");
            }
            return responseData.success(callCarConfigDTO);
        } catch (BeansException e) {
            log.error("e -> {}", ToStringBuilder.reflectionToString(e));
            return responseData.fail("系统繁忙");
        }
    }

    /**
     * @Author huchenying
     * @Description  删除叫车策略
     * @Date 2022/5/12 23:46
     **/
    @PostMapping("/deleteCallConfig")
    public ResponseData<Object> deleteCallConfig(@Validated(Delete.class) @RequestBody CallCarConfigDTO callCarConfigDTO){
        callCarConfig.removeById(callCarConfigDTO.getId());
        return ResponseData.success();
    }

    /**
     * -------------------------- 区域配置 ------------------------------
     */

    /**
     * @Author huchenying
     * @Description  获取区域配置list
     * @Date 2022/5/12 23:49
     **/
    @PostMapping("/getGridConfig")
    public ResponseData<List<GaoDeGfidsResponseDTO>> getGridConfig(){
        List<GaoDeGfidsResponseDTO> res = new ArrayList<>();
        ResponseData<List<GaoDeGfidsResponseDTO>> responseData = new ResponseData<>();
        List<GaodeGfids> gfids = commonInfoBz.getGfids();
        for (GaodeGfids gfid : gfids) {
            GaoDeGfidsResponseDTO gaoDeGfidsResponseDTO = new GaoDeGfidsResponseDTO();
            BeanUtils.copyProperties(gfid,gaoDeGfidsResponseDTO);
            res.add(gaoDeGfidsResponseDTO);
        }
        return responseData.success(res);
    }

    /**
     * @Author huchenying
     * @Description  添加更新区域配置
     * @Date 2022/5/12 23:50
     **/
    @PostMapping("/insertOrUpdateGridConfig")
    public ResponseData<GaoDeGfidsResponseDTO> insertOrUpdateGridConfig(@Validated(Add.class) @RequestBody GaoDeGfidsResponseDTO gaodeGfids){
        ResponseData<GaoDeGfidsResponseDTO> responseData = new ResponseData<>();
        GaodeGfids gfids = new GaodeGfids();
        BeanUtils.copyProperties(gaodeGfids,gfids);
        boolean b = gaodeGfidsService.saveOrUpdate(gfids);
        if(!b){
            return responseData.fail("添加失败");
        }
        return responseData.success(gaodeGfids);
    }

    /**
     * @Author huchenying
     * @Description  删除区域配置
     * @Date 2022/5/12 23:50
     **/
    @PostMapping("/deleteGridConfig")
    public ResponseData<Object> deleteGridConfig(@Validated(Delete.class) @RequestBody GaoDeGfidsResponseDTO gaodeGfids){

        boolean b = gaodeGfidsService.removeById(gaodeGfids.getId());
        return ResponseData.success();
    }


    /**
     * -------------------------- 价格配置 ------------------------------
     */

    /**
     * @Author huchenying
     * @Description  获取价格配置
     * @Date 2022/5/12 23:51
     **/
    @PostMapping("/getPriceConfig")
    public ResponseData<Object> getPriceConfig(){

        return ResponseData.success();
    }

    /**
     * @Author huchenying
     * @Description  更新价格配置
     * @Date 2022/5/12 23:51
     **/
    @PostMapping("/updatePriceConfig")
    public ResponseData<Object> updatePriceConfig(){

        return ResponseData.success();
    }
}
